<?php
/**
 * @修改：莫深辰
 * @时间:    2018-05-03 10:35:50
 * @功能: 仓库管理  同时添加职员
 */
header('Access-Control-Allow-Origin: *');
require( "config.php");

// insert
$op = $_POST['op'];
if($op == 'add'){

$data = $_POST;
$contact['number'] =$staff['number'] = $data['locationNo'] = time();
unset($data['op']);
 $id = $db->insert('ci_storage',$data);

//销售人/职员
 $contact['id'] =  $staff['id'] = $id;
$contact['name'] = $staff['name'] = $_POST['name'];
 $id2 = $db->insert('ci_staff',$staff);

//客户表
 $id3 = $db->insert('ci_contact',$contact);


 if($id && $id2 && $id3){
	$re['status'] = '200';
	$re['msg'] = $id;
 }else{
	$re['status'] = '110';
	$re['msg'] = '添加失败！';
 }
 die(json_encode($re));
 }

// update
if($op == 'update'){

$data = $_POST;
unset($data['op']);

 $where = 'id = '.$_POST['id'];
 $id = $db->update('ci_storage',$data,$where);
 $id2 = $db->update('ci_staff',$data,$where);

 if($id && $id2){
	$re['status'] = '200';
	$re['msg'] =  $op.' success!';
 }else{
	$re['status'] = '110';
	$re['msg'] = $op.' failed!';
 }
 	die(json_encode($re));
 }

//delete
 if($op == 'delete'){

 $id =  $_POST['id'];
 $where = 'id = '.$id;
 $id = $db->delete('ci_storage',$where);

 if($id){
	$re['status'] = '200';
	$re['msg'] =  $op.' success!';
 }else{
	$re['status'] = '110';
	$re['msg'] = $op.' failed!';;
 }
 	die(json_encode($re));
 }

//find
 if($op == 'find'){

 $id =  $_POST['id'];
 $where = 'id = '.$id;
  $db->find('ci_storage','*',$where);
  $re1 = $db->fetch_assoc();

 if($re1){
	$re['status'] = '200';
	$re['msg'] =  $re1;
 }else{
	$re['status'] = '110';
	$re['msg'] = $op.' failed!';;
 }
 	die(json_encode($re));
 }

//select
 if($op == 'select'){

 $where =  $_POST['where'];
 $re1 = $db->select('ci_storage','*',$where);

 if($re1){
	$re['status'] = '200';
	$re['msg'] =  $re1;
 }else{
	$re['status'] = '110';
	$re['msg'] = $op.' failed!';;
 }
 	die(json_encode($re));
 }